package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.BizTmpMapper;
import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntImageInfoMapper;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BizTmpMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntImageInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BizTmp;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.MchntImageInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.*;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.model.WechatIntegration;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.enums.BompImageUploadTypeEnum;
import cc.rengu.igas.bomp.facade.request.WechatRealNameAuditQueryRequest;
import cc.rengu.igas.bomp.facade.response.WechatRealNameAuditQueryResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.RealNameAuthQueryRequest;
import cc.rengu.igas.channel.wechat.facade.request.WechatQueryApplymentRequest;
import cc.rengu.igas.channel.wechat.facade.response.RealNameAuthQueryResponse;
import cc.rengu.igas.channel.wechat.facade.response.WechatQueryApplymentResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bank
 * @version 1.0
 * @Description: 微信实名认证申请审核结果查询
 * @date 2019/08/15
 */
public class WechatRealNameAuditQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        WechatRealNameAuditQueryRequest realNameAuditQueryRequest = new WechatRealNameAuditQueryRequest();
        ConvertUtil.convertOutput(realNameAuditQueryRequest);
        return realNameAuditQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        WechatRealNameAuditQueryRequest queryRequest = (WechatRealNameAuditQueryRequest) request;
        BizResponse<WechatRealNameAuditQueryResponse> bizResponse = new BizResponse<>();
        WechatRealNameAuditQueryResponse queryResponse = new WechatRealNameAuditQueryResponse();
        bizResponse.setResult(queryResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(queryRequest.getHeader().getInstId(), queryRequest.getMchntNo());
        if (null == mchntBaseInfo) {
            rglog.error("微信商户实名认证结果查询处理失败, 根据商户号查询商户基础信息为空.");
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        if (!MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
            rglog.error("商户状态不合法,status:<{}>", mchntBaseInfo.getMchntStatus());
            throw new BizException(RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespDesc());
        }
        mchntBaseInfo.setLastOprId(queryRequest.getUserId());

        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
        if (null == wechatMchntInfo) {
            rglog.error("微信商户实名认证结果查询处理失败, 根据商户号查询微信商户信息为空或微信商户号为空,请先进行第三方机构微信进件处理.");
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        rglog.info("微信商户信息:<{}>", JSONObject.toJSONString(wechatMchntInfo));
        if (!"01".equals(wechatMchntInfo.getWechatEntryFlag())) {
            rglog.error("未微信商户进件.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "未微信商户进件");
        }
        if (StringUtil.isEmptyOrNull(wechatMchntInfo.getWechatMchntNo())) {
            rglog.error("未微信商户进件.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "未微信商户进件");
        }
        if (AppParamConstant.NO.equals(wechatMchntInfo.getIndependentEntryFlag())) {
            rglog.error("非独立进件,无需实名认证.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "非独立进件,无需实名认证");
        }
        if (AppParamConstant.YES.equals(wechatMchntInfo.getRealNameFlag())) {
            rglog.error("已实名认证.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "已实名认证");
        } else if (AppParamConstant.NO.equals(wechatMchntInfo.getRealNameFlag())) {
            rglog.error("未实名认证申请.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "未实名认证申请");
        }
        if (StringUtil.isEmptyOrNull(wechatMchntInfo.getRealNameApplyNo())) {
            rglog.error("实名认证申请单号为空.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "实名认证申请单号为空");
        }

        if (WechatRealNameApplyStatusEnum.WECHAT_REAL_NAME_APPLYING.equals(wechatMchntInfo.getRealNameFlag())) {
            queryApplyment(queryResponse, mchntBaseInfo, wechatMchntInfo, sysSeqNum);
        } else if (WechatRealNameApplyStatusEnum.WECHAT_REAL_NAME_MCHNT_CONFIRMED.equals(wechatMchntInfo.getRealNameFlag())) {
            realNameAuthQuery(queryResponse, mchntBaseInfo, wechatMchntInfo, sysSeqNum);
        } else if (WechatRealNameApplyStatusEnum.WECHAT_REAL_NAME_REJECT.equals(wechatMchntInfo.getRealNameFlag())) {
            rglog.info("微信实名被拒绝.");
            queryResponse.setRealNameApplyStatus("10");
            queryResponse.setApplyRejectReason(wechatMchntInfo.getWechatRlNmAuthFalRsn());
        } else {
            rglog.error("商户<{}>实名认证状态<{}>异常.", wechatMchntInfo.getMchntNo(), wechatMchntInfo.getRealNameFlag());
            throw new BizException(RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespDesc());
        }

        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        WechatRealNameAuditQueryResponse wechatRealNameAuditQueryResponse = (WechatRealNameAuditQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(wechatRealNameAuditQueryResponse);
    }

    private void queryApplyment(WechatRealNameAuditQueryResponse queryResponse, MchntBaseInfo mchntBaseInfo, WechatMchntInfo wechatMchntInfo, String sysSeqNum) throws Exception {
        rglog.info("微信实名认证申请中.");
        WechatQueryApplymentRequest queryApplymentRequest = new WechatQueryApplymentRequest();
        Header header = InitParamUtil.initWechatHeader("queryapply", mchntBaseInfo.getInstId(), sysSeqNum);
        queryApplymentRequest.setHeader(header);
        queryApplymentRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
        queryApplymentRequest.setBusinessCode(wechatMchntInfo.getRealNameBizNo());
        queryApplymentRequest.setApplymentId(wechatMchntInfo.getRealNameApplyNo());
        queryApplymentRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        DubboService dubboService = new DubboServiceImpl();
        Result result = (Result) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "queryApplyment", queryApplymentRequest);
        if (null == result || !result.isSuccess() || null == result.getResult()) {
            rglog.error("调用失败.");
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        WechatQueryApplymentResponse wechatQueryApplymentResponse = (WechatQueryApplymentResponse) result.getResult();
        if (!DubboMethodConstant.DUBBO_SUCCESS.equals(wechatQueryApplymentResponse.getRespCode())) {
            rglog.error("调用错误<{}>-<{}>.", wechatQueryApplymentResponse.getRespCode(), wechatQueryApplymentResponse.getRespDesc());
            throw new BizException(wechatQueryApplymentResponse.getRespCode(), wechatQueryApplymentResponse.getRespDesc());
        }

        WechatIntegration wechatIntegration = new WechatIntegration();
        if (WechatRealNameApplyStatusEnum.APPLYMENT_STATE_PASSED.getWechatStatus().equals(wechatQueryApplymentResponse.getApplymentState())) {
            rglog.debug("审核通过");
            wechatMchntInfo.setRealNameFlag(WechatRealNameApplyStatusEnum.WECHAT_REAL_NAME_MCHNT_CONFIRMED);
            wechatMchntInfo.setLastOprId(mchntBaseInfo.getLastOprId());
            wechatMchntInfo.setLastUpdateTime(DateUtil.getCurrentDateTime(BompAppParamConstant.yyyy_MM_dd_HHmmssSSS));

            SysParam sysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), SysParamEnum.localStorageRootPath);
            if (null == sysParam) {
                rglog.error("服务器本地图片存储路径未配置.");
                throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "服务器本地图片存储路径未配置");
            }
            String uuid = RandomUtil.getUUID();
            String filePath = sysParam.getParamValue() + File.pathSeparator + BompImageUploadTypeEnum.REAL_NAME_QRCODE.getImagePathKey()
                    + mchntBaseInfo.getInstId() + File.pathSeparator + mchntBaseInfo.getMchntNo() + File.pathSeparator + uuid + ".jpg";

            //IO流操作
            OutputStream outputStream = null;
            try {
                File storageFile = new File(filePath);
                if (!storageFile.exists()) {
                    if (!storageFile.getParentFile().exists()) {
                        storageFile.getParentFile().mkdirs();
                    }
                    storageFile.createNewFile();
                }

                outputStream = new FileOutputStream(storageFile);
                byte[] fileBytes = Base64.decodeBase64(wechatQueryApplymentResponse.getQrcodeData());
                outputStream.write(fileBytes);
                outputStream.flush();
            } catch (Exception e) {
                rglog.error(e.getMessage());
                throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "保存商户实名认证二维码信息失败");
            } finally {
                //关闭流
                try {
                    if (null != outputStream) {
                        outputStream.close();
                    }
                } catch (Exception e) {
                    rglog.error(e.getMessage());
                }
            }

            int imageIndex = 0;
            MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
            List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNoAndImageType(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), ImageTypeEnum.REAL_NAME_QR_CODE.getImageType());
            if (!CollectionUtils.isEmpty(mchntImageInfoList)) {
                mchntImageInfoList = mchntImageInfoList.stream().sorted(Comparator.comparing(MchntImageInfo::getImageType)).collect(Collectors.toList());
                imageIndex = mchntImageInfoList.get(0).getImageIndex() + 1;
            }

            MchntImageInfo mchntImageInfo = new MchntImageInfo();
            BeanUtil.beanCopy(mchntBaseInfo, mchntImageInfo);
            mchntImageInfo.setImageType(ImageTypeEnum.REAL_NAME_QR_CODE.getImageType());
            mchntImageInfo.setImageIndex(imageIndex);
            mchntImageInfo.setImageLocalPath(BompImageUploadTypeEnum.REAL_NAME_QRCODE.getImagePathKey()
                    + mchntBaseInfo.getInstId() + File.pathSeparator + mchntBaseInfo.getMchntNo() + File.pathSeparator + uuid + ".jpg");
            mchntImageInfo.setImageDesc(wechatMchntInfo.getRealNameApplyNo());
            mchntImageInfo.setRecordStatus(AppParamConstant.YES);
            mchntImageInfo.setOprId(mchntBaseInfo.getLastOprId());
            mchntImageInfo.setCreateTime(DateUtil.getCurrentTime());
            String imageId = RandomUtil.getUUID();
            rglog.debug("实名认证影像<{}>ID<{}>.", ImageTypeEnum.REAL_NAME_QR_CODE.getImageType(), imageId);
            mchntImageInfo.setId(imageId);

            DbsUtil dbsUtil = InitParamUtil.dbsBeginTransaction(AppConfigConstant.MCMP_POOL_NAME);
            try {
                int dbResult = mchntImageInfoMapper.insertMchntImageInfo(mchntImageInfo);
                if (0 != dbResult) {
                    dbResult = mchntImageInfoMapper.updateMchntImageInfoById(mchntImageInfo);
                    if (0 != dbResult) {
                        rglog.error("保存商户影像失败.");
                        throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "保存商户实名认证二维码信息失败");
                    }
                }
                BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
                BizTmp bizTmp = InitParamUtil.initBizTmpInfo(mchntBaseInfo, TableNameConstant.T_B_MCHNT_IMAGE_INFO, BompBizOprEnum.INSERT.getBizOprCode(), mchntImageInfo);
                dbResult = bizTmpMapper.insertBizTmp(bizTmp);
                if (0 != dbResult) {
                    dbResult = bizTmpMapper.updateBizTmpByRealId(bizTmp);
                    if (0 != dbResult) {
                        rglog.error("保存商户影像失败.");
                        throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "保存商户实名认证二维码信息失败");
                    }
                }
                dbsUtil.dbsEndTransaction(true);
            } catch (Exception e) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error("保存商户实名认证二维码信息失败.");
                throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "保存商户实名认证二维码信息失败");
            }
            queryResponse.setQrcodeData(wechatQueryApplymentResponse.getQrcodeData());

            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            BizTmp wechatMchntInfoBizTmp = bizTmpMapper.selectBizTmpByRealId(wechatMchntInfo.getInstId(), wechatMchntInfo.getId());
            WechatMchntInfo bizTmpWechatMchntInfo = JSONObject.parseObject(InitParamUtil.getJsonString(wechatMchntInfoBizTmp), WechatMchntInfo.class);
            bizTmpWechatMchntInfo.setRealNameFlag(wechatMchntInfo.getRealNameFlag());
            bizTmpWechatMchntInfo.setLastOprId(wechatMchntInfo.getLastOprId());
            bizTmpWechatMchntInfo.setLastUpdateTime(wechatMchntInfo.getLastUpdateTime());
            InitParamUtil.initBizTempData(wechatMchntInfoBizTmp, JSONObject.toJSONString(bizTmpWechatMchntInfo));
            wechatIntegration.updateWechatMchntInfoAndBizTmp(wechatMchntInfo, wechatMchntInfoBizTmp);
        } else if (WechatRealNameApplyStatusEnum.APPLYMENT_STATE_REJECTED.getWechatStatus().equals(wechatQueryApplymentResponse.getApplymentState())
                || WechatRealNameApplyStatusEnum.APPLYMENT_STATE_FREEZED.getWechatStatus().equals(wechatQueryApplymentResponse.getApplymentState())
                || WechatRealNameApplyStatusEnum.APPLYMENT_STATE_CANCELED.getWechatStatus().equals(wechatQueryApplymentResponse.getApplymentState())) {
            rglog.info("认证失败<{}>", wechatQueryApplymentResponse.getApplymentState());
            wechatMchntInfo.setRealNameFlag(WechatRealNameApplyStatusEnum.WECHAT_REAL_NAME_REJECT);
            wechatMchntInfo.setWechatRlNmAuthFalRsn(null == wechatQueryApplymentResponse.getRejectReason() ? "已作废" : wechatQueryApplymentResponse.getRejectReason());
            wechatMchntInfo.setLastOprId(mchntBaseInfo.getLastOprId());
            wechatMchntInfo.setLastUpdateTime(DateUtil.getCurrentDateTime(BompAppParamConstant.yyyy_MM_dd_HHmmssSSS));

            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            BizTmp wechatMchntInfoBizTmp = bizTmpMapper.selectBizTmpByRealId(wechatMchntInfo.getInstId(), wechatMchntInfo.getId());
            WechatMchntInfo bizTmpWechatMchntInfo = JSONObject.parseObject(InitParamUtil.getJsonString(wechatMchntInfoBizTmp), WechatMchntInfo.class);
            bizTmpWechatMchntInfo.setRealNameFlag(wechatMchntInfo.getRealNameFlag());
            bizTmpWechatMchntInfo.setWechatRlNmAuthFalRsn(wechatMchntInfo.getWechatRlNmAuthFalRsn());
            bizTmpWechatMchntInfo.setLastOprId(wechatMchntInfo.getLastOprId());
            bizTmpWechatMchntInfo.setLastUpdateTime(wechatMchntInfo.getLastUpdateTime());
            InitParamUtil.initBizTempData(wechatMchntInfoBizTmp, JSONObject.toJSONString(bizTmpWechatMchntInfo));
            wechatIntegration.updateWechatMchntInfoAndBizTmp(wechatMchntInfo, wechatMchntInfoBizTmp);
            queryResponse.setApplyRejectReason(wechatQueryApplymentResponse.getRejectReason());
        } else {
            rglog.info("认证中<{}>", wechatQueryApplymentResponse.getApplymentState());
        }
        queryResponse.setRealNameApplyStatus(WechatRealNameApplyStatusEnum.getWechatRealNameApplyStatusEnumByWechatStatus(wechatQueryApplymentResponse.getApplymentState()));
    }

    private void realNameAuthQuery(WechatRealNameAuditQueryResponse queryResponse,
                                   MchntBaseInfo mchntBaseInfo, WechatMchntInfo wechatMchntInfo, String sysSeqNum) throws Exception {
        rglog.info("微信实名待商户确认.");
        RealNameAuthQueryRequest wechatQueryRequest = new RealNameAuthQueryRequest();
        Header queryHeader = InitParamUtil.initWechatHeader("authquery", mchntBaseInfo.getInstId(), sysSeqNum);
        wechatQueryRequest.setHeader(queryHeader);
        wechatQueryRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
        wechatQueryRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        wechatQueryRequest.setThirdMchntNo(wechatMchntInfo.getWechatMchntNo());
        DubboService dubboService = new DubboServiceImpl();
        Result result = (Result) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "realNameAuthQuery", wechatQueryRequest);
        if (null == result || !result.isSuccess() || null == result.getResult()) {
            rglog.error("调用服务失败.");
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        RealNameAuthQueryResponse realNameAuthQueryResponse = (RealNameAuthQueryResponse) result.getResult();
        if (!DubboMethodConstant.DUBBO_SUCCESS.equals(realNameAuthQueryResponse.getRespCode())) {
            rglog.error("调用错误<{}>-<{}>.", realNameAuthQueryResponse.getRespCode(), realNameAuthQueryResponse.getRespDesc());
            throw new BizException(realNameAuthQueryResponse.getRespCode(), realNameAuthQueryResponse.getRespDesc());
        } else {
            if (WechatRealNameApplyStatusEnum.AUTHORIZE_STATE_AUTHORIZED.equals(realNameAuthQueryResponse.getAuthorizeState())) {
                rglog.info("微信已授权");
                wechatMchntInfo.setRealNameFlag(AppParamConstant.YES);
                wechatMchntInfo.setLastOprId(mchntBaseInfo.getLastOprId());
                wechatMchntInfo.setLastUpdateTime(DateUtil.getCurrentDateTime(BompAppParamConstant.yyyy_MM_dd_HHmmssSSS));
                wechatMchntInfo.setLastRealTime(wechatMchntInfo.getLastUpdateTime());

                BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
                BizTmp wechatMchntInfoBizTmp = bizTmpMapper.selectBizTmpByRealId(wechatMchntInfo.getInstId(), wechatMchntInfo.getId());
                WechatMchntInfo bizTmpWechatMchntInfo = JSONObject.parseObject(InitParamUtil.getJsonString(wechatMchntInfoBizTmp), WechatMchntInfo.class);
                bizTmpWechatMchntInfo.setRealNameFlag(wechatMchntInfo.getRealNameFlag());
                bizTmpWechatMchntInfo.setLastOprId(wechatMchntInfo.getLastOprId());
                bizTmpWechatMchntInfo.setLastUpdateTime(wechatMchntInfo.getLastUpdateTime());
                bizTmpWechatMchntInfo.setLastRealTime(wechatMchntInfo.getLastRealTime());
                InitParamUtil.initBizTempData(wechatMchntInfoBizTmp, JSONObject.toJSONString(bizTmpWechatMchntInfo));

                WechatIntegration wechatIntegration = new WechatIntegration();
                wechatIntegration.updateWechatMchntInfoAndBizTmp(wechatMchntInfo, wechatMchntInfoBizTmp);
                queryResponse.setRealNameApplyStatus("03");
            } else if (WechatRealNameApplyStatusEnum.AUTHORIZE_STATE_UNAUTHORIZED.equals(realNameAuthQueryResponse.getAuthorizeState())) {
                rglog.info("微信未授权");
            } else {
                rglog.error("未知返回码<{}>", realNameAuthQueryResponse.getAuthorizeState());
            }
        }
    }
}
